Python NumPy Basics CheatSheet

The NumPy library is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.


In [2]:
import numpy as np

Help


In [2]:
np.info(np.ndarray.dtype)


Data-type of the array's elements.

Parameters
----------
None

Returns
-------
d : numpy dtype object

See Also
--------
numpy.dtype

Examples
--------
>>> x
array([[0, 1],
       [2, 3]])
>>> x.dtype
dtype('int32')
>>> type(x.dtype)
<type 'numpy.dtype'>

Creating Arrays


In [6]:
a = np.array([1,2,3])
print("a = \n{}".format(a))
b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
print("b = \n{}".format(b))
c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype = float)
print("c = \n{}".format(c))


a = 
[1 2 3]
b = 
[[1.5 2.  3. ]
 [4.  5.  6. ]]
c = 
[[[1.5 2.  3. ]
  [4.  5.  6. ]]

 [[3.  2.  1. ]
  [4.  5.  6. ]]]

Initial Placeholders


In [7]:
zeros_arr = np.zeros((3,4))                # Create an array of zeros
print("zeros_arr = \n{}".format(zeros_arr))
ones_arr = np.ones((2,3,4),dtype=np.int16) # Create an array of ones
print("ones_arr = \n{}".format(ones_arr))
d = np.arange(10,25,5)                     # Create an array of evenly spaced values (step value)
print("d = {}".format(d))
linspace_arr = np.linspace(0,2,9)          # Create an array of evenly spaced values (number of samples)
print("linspace_arr = \n{}".format(linspace_arr))
e = np.full((2,2),7)                       # Create a constant array
print("e = \n{}".format(e))
f = np.eye(2)                              # Create a 2X2 identity matrix
print("f = \n{}".format(f))
random_arr = np.random.random((2,2))       # Create an array with random values
print("random_arr = \n{}".format(random_arr))
empty_arr = np.empty((3,2))                # Create an empty array
print("empty_arr = \n{}".format(empty_arr))


zeros_arr = 
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
ones_arr = 
[[[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]

 [[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]]
d = [10 15 20]
linspace_arr = 
[0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]
e = 
[[7 7]
 [7 7]]
f = 
[[1. 0.]
 [0. 1.]]
random_arr = 
[[0.19538833 0.37218249]
 [0.20691142 0.50623184]]
empty_arr = 
[[1.39069238e-309 1.39069238e-309]
 [1.39069238e-309 1.39069238e-309]
 [1.39069238e-309 1.39069238e-309]]

I/O

Saving & Loading On Disk


In [10]:
np.save('../sample_files/data/numpy_array', a)         # Saves a binary file
np.savez('../sample_files/data/numpy_array.npz', a, b) # Saves uncompressed npz
np.load('../sample_files/data/numpy_array.npy')        # Load file


Out[10]:
array([1, 2, 3])

Saving & Loading Text Files


In [13]:
np.loadtxt("../sample_files/data/mnist.txt")
np.genfromtxt("../sample_files/data/winequality-red.csv", delimiter=',')
np.savetxt("../sample_files/data/numpy.txt", b, delimiter=" ")

Data Types


In [14]:
np.int64    # Signed 64-bit integer types
np.float32  # Standard double-precision floating point
np.complex  # Complex numbers represented by 128 floats
np.bool     # Boolean type storing TRUE and FALSE values
np.object   # Python object type
np.string_  # Fixed-length string type
np.unicode_ # Fixed-length unicode type


Out[14]:
numpy.str_

Inspecting Array


In [21]:
ans = a.shape       # Array dimensions
print("a.shape = {}".format(ans))
ans = len(a)        # Length of array
print("len(a) = {} elements".format(ans))
ans = b.ndim        # Number of array dimensions
print("b.ndim = {} dimensions".format(ans))
ans = e.size        # Number of array elements
print("e.size = {} elements".format(ans))
ans = b.dtype       # Data type of array elements
print("b.dtype = {}".format(ans))
ans = b.dtype.name  # Name of data type
print("b.dtype.name = {}".format(ans))
ans = b.astype(int) # Convert an array to a different type
print("b.astype(int) = \n{}".format(ans))


a.shape = (3,)
len(a) = 3
b.ndim = 2
e.size = 4
b.dtype = float64
b.dtype.name = float64
b.astype(int) = 
[[1 2 3]
 [4 5 6]]

Array Mathematics

Arithmetic Operations


In [23]:
g = a - b        # Subtraction
np.subtract(a,b) # Subtraction

b + a            # Addition
np.add(b,a)      # Addition

a / b            # Division
np.divide(a,b)   # Division

a * b            # Multiplication
np.multiply(a,b) # Multiplication

np.exp(b)        # Exponentiation
np.sqrt(b)       # Square root
np.sin(a)        # Print sines of an array
np.cos(b)        # Element-wise cosine
np.log(a)        # Element-wise natural logarithm
e.dot(f)         # Dot product


Out[23]:
array([[7., 7.],
       [7., 7.]])

Comparison


In [29]:
ans = a == b               # Element-wise comparison
print("a == b is \n{}".format(ans))
ans = a < 2                # Element-wise comparison
print("a < 2 is {}".format(ans))
ans = np.array_equal(a, b) # Array-wise comparison
print("np.array_equal(a, b) is {}".format(ans))


a == b is 
[[False  True  True]
 [False False False]]
a < 2 is [ True False False]
np.array_equal(a, b) is False

Aggragate Functions


In [36]:
ans = a.sum()          # Array-wise sum
print("a.sum() = {}".format(ans))
ans = a.min()          # Array-wise minimum value
print("a.min() = {}".format(ans))
ans = b.max(axis=0)    # Maximum value of an array row
print("b.max(axis=0) = {}".format(ans))
ans = b.cumsum(axis=1) # Cumulative sum of the elements
print("b.cumsum(axis=1) = \n{}".format(ans))
ans = a.mean()         # Mean
print("a.mean() = {}".format(ans))
ans = np.median(b)       # Median
print("np.median(b) = {}".format(ans))
ans = np.corrcoef(a)     # Correlation coefficient
print("np.corrcoef(a) = {}".format(ans))
ans = np.std(b)        # Standard deviation
print("np.std(b) = {}".format(ans))


a.sum() = 6
a.min() = 1
b.max(axis=0) = [4. 5. 6.]
b.cumsum(axis=1) = 
[[ 1.5  3.5  6.5]
 [ 4.   9.  15. ]]
a.mean() = 2.0
np.median(b) = 3.5
np.corrcoef(a) = 1.0
np.std(b) = 1.5920810978785667

Copying Arrays


In [37]:
h = a.view() # Create a view of the array with the same data
np.copy(a)   # Create a copy of the array
h = a.copy() # Create a deep copy of the array

Sorting Arrays


In [48]:
a.sort()       # Sort an array
print("a.sort() = {}".format(a))
c.sort(axis=0) # Sort the elements of an array's axis
print("c.sort(axis=0) = \n{}".format(c))


a.sort() = [1 2 3]
c.sort(axis=0) = 
[[[1.5 2.  1. ]
  [4.  5.  6. ]]

 [[3.  2.  3. ]
  [4.  5.  6. ]]]

Subsetting & Slicing & Indexing

Index starts at 0


In [64]:
# Subsetting
ans = a[2]      # Select the element at the 2nd index
print("a={} => a[2]={}".format(a, ans))
ans = b[1,2]    # Select the element at row 1 column 2
print("b=\n{} => b[1,2]={}".format(b, ans))

# Slicing
ans = a[0:2]    # Select items at index 0 and 1
print("a={} => a[0:2]={}".format(a, ans))
ans = b[0:2,1]  # Select items at rows 0 and 1 in column 1
print("b={} => b[0:2,1]={}".format(b, ans))
ans = b[:1]     # Select all items at row 0
print("b={} => b[:1]={}".format(b, ans))
ans = c[1,:,:]  # Same as [1,...]
print("c=\n{} =>\nc[1,:,:]=\n{}".format(c, ans))
ans = a[ : :-1] # Reversed array a
print("a={} => a[ : :-1]={}".format(a, ans))

# Boolean Indexing
ans = a[a<2]    # Select elements from a less than 2
print("a={} => a[a<2]={}".format(a, ans))


# Fancy Indexing
ans = b[[1, 0, 1, 0],[0, 1, 2, 0]] # Select elements (1,0),(0,1),(1,2) and (0,0)
print("b=\n{} =>\nb[1, 0, 1, 0],[0, 1, 2, 0]]=\n{}".format(b, ans))
ans = b[[1, 0, 1, 0]][:,[0,1,2,0]] # Select a subset of the matrix’s rows
print("b=\n{} =>\nb[[1, 0, 1, 0]][:,[0,1,2,0]]=\n{}".format(b, ans))


a=[1 2 3] => a[2]=3
b=
[[1.5 2.  3. ]
 [4.  5.  6. ]] => b[1,2]=6.0
a=[1 2 3] => a[0:2]=[1 2]
b=[[1.5 2.  3. ]
 [4.  5.  6. ]] => b[0:2,1]=[2. 5.]
b=[[1.5 2.  3. ]
 [4.  5.  6. ]] => b[:1]=[[1.5 2.  3. ]]
c=
[[[1.5 2.  1. ]
  [4.  5.  6. ]]

 [[3.  2.  3. ]
  [4.  5.  6. ]]] =>
c[1,:,:]=
[[3. 2. 3.]
 [4. 5. 6.]]
a=[1 2 3] => a[ : :-1]=[3 2 1]
a=[1 2 3] => a[a<2]=[1]
b=
[[1.5 2.  3. ]
 [4.  5.  6. ]] =>
b[1, 0, 1, 0],[0, 1, 2, 0]]=
[4.  2.  6.  1.5]
b=
[[1.5 2.  3. ]
 [4.  5.  6. ]] =>
b[[1, 0, 1, 0]][:,[0,1,2,0]]=
[[4.  5.  6.  4. ]
 [1.5 2.  3.  1.5]
 [4.  5.  6.  4. ]
 [1.5 2.  3.  1.5]]

Array Manipulation


In [82]:
# Transposing Array
ans = np.transpose(b)       # Permute array dimensions
print("np.transpose(b) =\n{}".format(ans))
ans = ans.T                 # Permute array dimensions
print("ans.T =\n{}".format(ans))

# Changing Array Shape
ans = b.ravel()           # Flatten the array
print("b.ravel() =\n{}".format(ans))

ans = g.reshape(3,-2)     # Reshape, but don’t change data
print("g.reshape(3,-2) =\n{}".format(ans))

# Adding/Removing Elements
h.resize((2,6))     # Return a new array with shape (2,6)
print("h.resize((2,6)) =\n{}".format(h))
ans = np.append(h,g)      # Append items to an array
print("np.append(h,g) =\n{}".format(ans))
ans = np.insert(a, 1, 5)  # Insert items in an array
print("np.insert(a, 1, 5) = {}".format(ans))
ans = np.delete(a,[1])    # Delete items from an array
print("np.delete(a,[1]) = {}".format(ans))

# Combining Arrays
ans = np.concatenate((a,d),axis=0) # Concatenate arrays
print("np.concatenate((a,d),axis=0) =\n{}".format(ans))
ans = np.vstack((a,b))    # Stack arrays vertically (row-wise)
print("np.vstack((a,b)) =\n{}".format(ans))
ans = np.r_[e,f]          # Stack arrays vertically (row-wise)
print("np.r_[e,f] =\n{}".format(ans))
ans = np.hstack((e,f))    # Stack arrays horizontally (column-wise)
print("np.hstack((e,f)) =\n{}".format(ans))
ans = np.column_stack((a,d)) # Create stacked column-wise arrays
print("np.column_stack((a,d)) =\n{}".format(ans))
ans = np.c_[a,d]          # Create stacked column-wise arrays
print("np.c_[a,d] =\n{}".format(ans))

# Splitting Arrays
ans = np.hsplit(a,3)      # Split the array horizontally at the 3rd
print("np.hsplit(a,3) =\n{}".format(ans))
ans = np.vsplit(c,2)      # Split the array vertically at the 2nd index
print("np.vsplit(c,2) =\n{}".format(ans))


np.transpose(b) =
[[1.5 4. ]
 [2.  5. ]
 [3.  6. ]]
ans.T =
[[1.5 2.  3. ]
 [4.  5.  6. ]]
b.ravel() =
[1.5 2.  3.  4.  5.  6. ]
g.reshape(3,-2) =
[[-0.5  0. ]
 [ 0.  -3. ]
 [-3.  -3. ]]
h.resize((2,6)) =
[[1 2 3 0 0 0]
 [0 0 0 0 0 0]]
np.append(h,g) =
[ 1.   2.   3.   0.   0.   0.   0.   0.   0.   0.   0.   0.  -0.5  0.
  0.  -3.  -3.  -3. ]
np.insert(a, 1, 5) = [1 5 2 3]
np.delete(a,[1]) = [1 3]
np.concatenate((a,d),axis=0) =
[ 1  2  3 10 15 20]
np.vstack((a,b)) =
[[1.  2.  3. ]
 [1.5 2.  3. ]
 [4.  5.  6. ]]
np.r_[e,f] =
[[7. 7.]
 [7. 7.]
 [1. 0.]
 [0. 1.]]
np.hstack((e,f)) =
[[7. 7. 1. 0.]
 [7. 7. 0. 1.]]
np.column_stack((a,d)) =
[[ 1 10]
 [ 2 15]
 [ 3 20]]
np.c_[a,d] =
[[ 1 10]
 [ 2 15]
 [ 3 20]]
np.hsplit(a,3) =
[array([1]), array([2]), array([3])]
np.vsplit(c,2) =
[array([[[1.5, 2. , 1. ],
        [4. , 5. , 6. ]]]), array([[[3., 2., 3.],
        [4., 5., 6.]]])]

In [ ]: